<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace custom;

/**
 * Description of Push
 *
 * @author wang
 */
class Push {
    private $sign_key = "jyzy2&gl*aq!98&^rf##@!&%^&`tAWqe";	//签名的key
    private $pass_key = "i7)*12K3!sUg^0#k";//加密的key
    private $version = "1.0";//接口版本
    private $signtype = "MD5";//加签方式
    private $charset = "UTF-8";//字符编码
    
    /**
     * 推送通知
     * @param type $url     推送地址
     * @param type $data    推送数据：json格式
     * @return string
     */
    public function push($url, $data){
        // 获取HOST:PORT
        $arr = parse_url($url);
        $host = $arr['host'];
        if (isset($arr['port'])){
            $host .= ":" . $arr['port'];
        }

        $arr = array();
        $arr['requestTime'] = time();
        $arr['version'] = $this->version;//固定值
        $arr['signType'] = $this->signtype;//固定值
        $arr['charset'] = $this->charset;//固定值
        $arr['bizContent'] = $data;
        
	    //加密
	    $arr['bizContent'] = @$this->getEncrypt($arr['bizContent']);

	    //加签
        @ksort($arr);//排序
        $arr['sign'] = @$this->getSignMsg($arr);
        $post_data = json_encode($arr);
        
        // 发送头
        $header = array(
            "HOST: " . $host,  //测试
            "Content-Type: application/json",  //文件post
            "Content-Length: " . strlen($post_data),
            "Connection: Keep-Alive",   
            "Charset: UTF-8",
        );

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
        curl_setopt($ch, CURLOPT_TIMEOUT,60);
        $output = curl_exec($ch);
        curl_close($ch);
        //返回数组
        if(empty($output)){
            return "";
        }else{
            //返回的信息中可能存在bom头信息，所以使用trim($str, chr(239).chr(187).chr(191))解决
            $output = trim($output, chr(239).chr(187).chr(191));
            return json_decode($output,true);
        }
        

    }
    
    /**
     * 
     * @param type $data
     */
    public function parse( $data){
        $result = "";
//	$data = json_decode($data, true);
	@ksort($data);//排序
        
	//验签
	if(@$this->checkSignMsg($data)){
            //解密
            $result = @$this->getDecrypt($data['bizContent']);
	}else{
            return "";
	}
        return $result;
    }
    
    //加签
    private function getSignMsg($pay_params = array()){
        $params_str = "";
        $signMsg = "";
        foreach ( $pay_params as $key => $val ) {
            if ($key != "sign" && $key != "signType" && $key != "charset" && isset ( $val ) && @$val != "") {
                $params_str .= $key . "=" . $val . "&";
            }
        }
        $params_str = substr ($params_str, 0, - 1 );
        $params_str = $params_str . $this->sign_key;
        $signMsg = strtolower ( md5 ( $params_str ) );
        return $signMsg;
    }
    
    //验签
    private function checkSignMsg($pay_params = array()){
        $params_str = "";
        $signMsg = "";
        foreach ( $pay_params as $key => $val ) {
            if ($key != "sign" && $key != "signType" && $key != "charset" && ! is_null ( $val ) && @$val != "") {
                $params_str .= "&" . $key . "=" . $val;
            }
        }
        if ($params_str){
            $params_str = substr ( $params_str, 1 );
        }
        $params_str = $params_str . $this->sign_key;
        $signMsg = strtolower ( md5 ( $params_str ));
        
        $return = (@$signMsg == @strtolower( $pay_params ['sign'] )) ? true : false;
        return $return;
    }

    //加密
    private function getEncrypt($str){
        $aes = new \custom\CryptAES();
        $aes->set_key($this->pass_key);
        $aes->require_pkcs5();
        $return = $aes->encrypt($str);
        return $return;
    }

    //解密
    private function getDecrypt($str){
        $aes = new \custom\CryptAES();
        $aes->set_key($this->pass_key);
        $aes->require_pkcs5();
        $return = $aes->decrypt($str);
        return $return;
    }
    
}
